package com.lancoo.znbkxx.teacher.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.view.View
import com.chad.library.adapter.base.BaseSectionMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.model.data.PaperSetSection
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_finished
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_high_score
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_papername
import kotlinx.android.synthetic.main.item_paper_finished.view.tv_total
import kotlinx.android.synthetic.main.item_paper_go_on.view.*
import kotlinx.android.synthetic.main.item_paper_head.view.*
import kotlinx.android.synthetic.main.item_paper_unpublished.view.*
import java.text.DecimalFormat

//import kotlinx.android.synthetic.main.item_paper_unpublished.view.v_divider as v_divider1


/**
 * @author : Admin
 * @date   : 2019/7/23 20:50m
 * @desc   :
 */
class PaperListAdapter(sectionHeadResId: Int, data: MutableList<PaperSetSection>) :
        BaseSectionMultiItemQuickAdapter<PaperSetSection, BaseViewHolder>(sectionHeadResId, data) {

    init {
        addItemType(1, R.layout.item_paper_finished)
        addItemType(2, R.layout.item_paper_go_on)
        addItemType(3, R.layout.item_paper_unpublished)
    }

    override fun convertHead(helper: BaseViewHolder?, item: PaperSetSection?) {
        with(helper!!.itemView) {
            isClickable = false
            if (helper.layoutPosition == 1 || helper.layoutPosition == 0) {
                v_head.visibility = View.GONE
            } else {
                v_head.visibility = View.VISIBLE
            }
            when (item!!.layer) {
                0, 1, 2 -> iv_head.setImageResource(R.mipmap.icon_paper_section_basic)
                3 -> iv_head.setImageResource(R.mipmap.icon_paper_section_textbook)
                4 -> iv_head.setImageResource(R.mipmap.icon_paper_section_topic)
            }
            tv_set_name?.text = item.paperSet?.SetName
            tv_set_size?.text = if (item.paperSet!!.Flag > 2) HtmlUtils.setText(
                mContext.resources.getString(R.string.producer, item.paperSet?.ProducerName)) else HtmlUtils.setText(
                mContext.resources.getString(R.string.list_child_paper_infor,
                    item.paperSet?.ReleaseCount, item.paperSet?.PredictCount))
        }
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: PaperSetSection?) {
        with(helper) {
            //            if (item!!.t.EvalCount != 0) {
            addOnClickListener(R.id.tv_comment)
            //            }
            //            if (item.t.State != 2) {
            addOnClickListener(R.id.tv_detail)
            //            }
            addOnClickListener(R.id.tv_publish)
            addOnClickListener(R.id.textView18)
            addOnClickListener(R.id.tv_total)
            addOnClickListener(R.id.tv_finished)
            addOnClickListener(R.id.tv_time)
        }
        with(helper.itemView) {
            //            if (helper.layoutPosition == size + i) {
            //                v_divider.visibility = View.GONE
            //            } else {
            //                v_divider.visibility = View.VISIBLE
            //            }
            val paperName = item!!.t.PaperName.replace("（", "(").replace("）", ")")
            val str = SpannableStringBuilder(paperName)
            val span = ForegroundColorSpan(Color.parseColor("#999999"))
            str.setSpan(span, paperName.lastIndexOf("("), paperName.length,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            tv_papername.text = str
            if (helper.itemViewType == 1 || helper.itemViewType == 2) {
                if (item.t.EvalCount == 0) {
                    tv_comment.alpha = 0.3f
                } else {
                    tv_comment.alpha = 1.0f
                }
                tv_average_score.text = "平均分:${item.t.AverageScore}"
                tv_high_score.text = "最高分:${item.t.HighestScore}"
                tv_finished.text = item.t.SubmitCount.toString()
                tv_total.text = "/${item.t.StuCount}"
                if (helper.itemViewType == 2) {
                    if (item.t.State == 2) {
                        //                            view6.alpha = 0.3f
                        tv_detail.alpha = 0.3f
                    } else {
                        //                            view6.alpha = 1.0f
                        tv_detail.alpha = 1.0f
                    }
                    //                        pb.progressDrawable = mContext.getDrawable(R.drawable.shape_progressbar_mini_green)
                    tv_time.text = item.t.StartTime.replace("-", ".")
                        .substring(0, item.t.StartTime.length - 9) + "~" + item.t.EndTime.replace(
                        "-", ".").substring(0, item.t.EndTime.length - 9)
                }
            } else if (helper.itemViewType == 3) {
                if (item.t.Layer == 3 || item.t.Layer == 4) {
                    //                    tv_average_score.visibility=View.GONE
                    tv_high_score.visibility = View.GONE
                    tv_effective.visibility = View.GONE
                    textView11.visibility = View.GONE
                    //                    textView12.visibility=View.GONE
                    textView14.visibility = View.GONE
                } else {
                    tv_average_score.visibility = View.VISIBLE
                    tv_high_score.visibility = View.VISIBLE
                    tv_effective.visibility = View.VISIBLE
                    textView11.visibility = View.VISIBLE
                    textView12.visibility = View.VISIBLE
                    textView14.visibility = View.VISIBLE
                }
                tv_average_score.text = "${item.t.PaperQualityAnalyze.PaperKnowledgeRate}%"
                tv_high_score.text = "${item.t.PaperQualityAnalyze.PaperMatchRate}%"
                tv_effective.text = "${item.t.PaperQualityAnalyze.PaperEffective}%"
                tv_difficult.text = HtmlUtils.setText(
                    mContext.resources.getString(R.string.difficult,
                        item.t.PaperQualityAnalyze.PaperDifficulty))
                tv_discrimination.text = HtmlUtils.setText(
                    mContext.resources.getString(R.string.discrimination,
                        item.t.PaperQualityAnalyze.PaperDistinguish))
                tv_exposure.text = HtmlUtils.setText(mContext.resources.getString(R.string.exposure,
                    item.t.PaperQualityAnalyze.PaperExposureRate))
            }
        }
    }


}